# Direct outputs eligible for public release

# GLOBAL SETTINGS --------------------------------------------------------------

options(
    scipen = 999,
    digits = 16,
    max.print = .Machine$integer.max,
    show.error.locations = TRUE,
    warn = 1
)

RNGkind("L'Ecuyer-CMRG")
seed <- 818675309L
set.seed(seed) # setting main seed

# PACKAGES ---------------------------------------------------------------------
library(data.table)
library(ggplot2)
library(scales)

# PACKAGE SETTINGS -------------------------------------------------------------

# data.table
setDTthreads(threads = 1L)
options(datatable.print.class = TRUE, datatable.print.keys = TRUE)
# so that printing the data.table also shows the variable type on top

# BEGIN FILE -------------------------------------------------------------------

# Read in all estimates

transition_to_high_se <-
    readRDS(
        "~/estimation-output/wealth_effect_estimates_transition_to_high_se.rds"
    )
transition_to_high_se <-
    transition_to_high_se[
        model == "reduced_form" & ref_event_time %in% c(-7:5)
    ]
transition_to_high_se[, outcome_label := "High-Paying Self Employment"]

transition_to_low_se <-
    readRDS(
        "~/estimation-output/wealth_effect_estimates_transition_to_low_se.rds"
    )
transition_to_low_se <-
    transition_to_low_se[
        model == "reduced_form" & ref_event_time %in% c(-7:5)
    ]
transition_to_low_se[, outcome_label := "Low-Paying Self Employment"]

transitions <-
    rbindlist(
        list(transition_to_high_se, transition_to_low_se),
        use.names = TRUE
    )

# Add omitted
temp <-
    CJ(
        outcome_label = unique(transitions$outcome_label),
        model = "reduced_form",
        income_quartile = 5
    )
temp[, ref_event_time := -2]
temp[, estimate := 0]
temp[, cluster_se := 0]

transitions <- rbindlist(list(transitions, temp), use.names = TRUE)
temp <- NULL
rm(temp)

transitions[
    ,
    outcome_label :=
        factor(
            outcome_label,
            levels = c("Low-Paying Self Employment", "High-Paying Self Employment") #nolint
        )
]

# Produce Figure B.11c
# Transitions into SE (percentage points)
transitions[, estimate := estimate * 100]
transitions[, cluster_se := cluster_se * 100]

figure_b_11_c <-
    ggplot(
        aes(
            x = ref_event_time,
            y = estimate,
            color = factor(outcome_label),
            fill = factor(outcome_label)
        ),
        data = transitions
    ) +
    geom_line() +
    geom_ribbon(
        aes(
            ymin = estimate - (1.64 * cluster_se),
            ymax = estimate + (1.64 * cluster_se)
        ),
        linetype = 0,
        alpha = 0.2,
        show.legend = FALSE
    ) +
    theme_bw(base_size = 13) +
    theme(panel.grid.minor = element_blank()) +
    scale_x_continuous(
        breaks = seq.int(from = -7L, to = 5L, by = 1L),
        expand = expansion(mult = c(0.0025, 0.0025))
    ) +
    scale_y_continuous(breaks = seq.int(from = -25, to = 125, by = 25) / 100) +
    labs(x = "Event Time (ℓ)", y = "Event Study Estimate (pp)") +
    theme(
        legend.title = element_blank(),
        legend.position = c(0.22, 0.917),
        legend.background = element_rect(fill = "white", color = "grey"),
        strip.text.x = element_blank(),
        strip.background = element_blank(),
        legend.key = element_rect(NA),
        legend.spacing.y = unit(2, "mm"),
        legend.key.height = unit(4, "mm"),
        legend.key.width = unit(4, "mm"),
        legend.margin = margin(t = 0, r = 0.1, b = 0.15, l = 0.1, unit = "cm")
    ) +
    scale_color_viridis_d() +
    scale_fill_viridis_d() +
    guides(color = guide_legend(ncol = 1, byrow = TRUE)) +
    coord_cartesian(ylim = c(-0.25, 1.25))

ggsave(
    plot = figure_b_11_c,
    filename = "~/paper/figures/figure-B.11c.png",
    width = 6,
    height = 4,
    dpi = 600
)

ggsave(
    plot = figure_b_11_c,
    filename = "~/paper/figures/figure-B.11c.tif",
    width = 6,
    height = 4,
    device = "tiff",
    dpi = 600
)

# Housekeeping
transition_to_high_se <- NULL
transition_to_low_se <- NULL
transitions <- NULL
figure_b_11_c <- NULL
rm(transition_to_high_se, transition_to_low_se, transitions, figure_b_11_c)